package com.glq1218.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.glq1218.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 验证码
 */
@RestController
public class CaptchaController {
    @Autowired
    private RedisUtils redisUtils;

    @GetMapping("/captcha")
    public void captcha(String sign, HttpServletResponse response) throws IOException {
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(120, 50);
        lineCaptcha.write(response.getOutputStream());
        String captchaCode = lineCaptcha.getCode();
        redisUtils.set("captcha:" + sign, captchaCode, 300);
    }

}
